//
//  LYChangePasswordView.m
//  SZBank
//
//  Created by LuoYao on 16/7/14.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "LYChangePasswordView.h"

@interface LYChangePasswordView ()
{
    NSMutableDictionary *_dic;
}
@end
@implementation LYChangePasswordView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _dic = [NSMutableDictionary dictionary];
        
        
        //背景图片
        UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _cKwidth * 0.4, _cKheight * 0.4)];
        backImageView.center = self.center;
        backImageView.userInteractionEnabled = YES;
        backImageView.image = [LocalDataManager getImageWithSourceOfPath:@"修改密码_03"];
        [self addSubview:backImageView];
        
        
        //输入框
        for (int i = 0; i < 3; i ++) {
            UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(backImageView.frame.size.width * 0.35, backImageView.frame.size.height * 0.265 + i * backImageView.frame.size.height * 0.16, backImageView.frame.size.width * 0.4, backImageView.frame.size.height * 0.1)];
            tf.background = [LocalDataManager getImageWithSourceOfPath:@"修改密码_14"];
            tf.tag = 1000 + i;
            tf.secureTextEntry = YES;
            [tf addTarget:self action:@selector(textFieldInputStr:) forControlEvents:UIControlEventEditingChanged];
            [backImageView addSubview:tf];
        }
        
        
        //btn
        NSArray *btnTitle = @[@"修改密码_07",@"修改密码_10"];
        for (int i = 0; i < btnTitle.count; i ++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(backImageView.frame.size.width * 0.3 + i * backImageView.frame.size.width * 0.25, backImageView.frame.size.height * 0.8, backImageView.frame.size.width * 0.2, backImageView.frame.size.height * 0.1)];
            btn.tag = 1000 + i + 3;
            [btn setImage:[LocalDataManager getImageWithSourceOfPath:btnTitle[i]] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(action_btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [backImageView addSubview:btn];
        }
        
        
    }
    return self;
}



#pragma mark - 输入的值获取
- (void)textFieldInputStr:(UITextField *)sender{
    [_dic setObject:sender.text forKey:[NSString stringWithFormat:@"%ld",sender.tag - 1000]];
}


- (void)action_btnClick:(UIButton *)sender{
    
    NSString *oldPassword = _dic[@"0"];
    NSString *newPassword = _dic[@"1"];
    NSString *newPassword2 = _dic[@"2"];
    
    
    
    
    switch (sender.tag - 1000) {
        case 3:
        {
            
            if (oldPassword.length == 0 || newPassword.length == 0 || newPassword2.length == 0) {
                //不能为空
                [self alertMessageWithMessageStr:@"不能为空"];
                return;
            }
            
            if (![newPassword2 isEqualToString:newPassword]) {
                //前后不一致
                [self alertMessageWithMessageStr:@"密码前后不一致"];
                return;
            }
            
            
            //从单例获取当前登录的用户名
            NSString *loginName = [PersonInfoSingle defaultPersonInfo].loginPersonInfo[@"LOGINNAME"];
            
            NSDictionary *dic = @{@"username":loginName,@"oldpass":oldPassword,@"newpass":newPassword2};
            
            [NetWorkingDataManager getProtocolManagerUpdatepassWithParam:dic success:^(id sender) {
                if ([sender[@"status"] isEqualToString:@"success"]) {
                    [self alertMessageWithMessageStr:sender[@"statusinfo"]];
                    [self dismissAnimation];
                }else{
                    [self alertMessageWithMessageStr:sender[@"statusinfo"]];
                }
   
            } failure:^(id sender) {
                [self alertMessageWithMessageStr:@"连接超时"];
            }];
        }
            break;
            
        case 4:
        {
            [self dismissAnimation];
        }
            break;
        default:
            break;
    }
    
}


#pragma mark - 弹出窗口
- (void)alertMessageWithMessageStr:(NSString *)str{
    [LocalDataManager addAlertControllerWithControllerTitle:@"提示" controllerMessage:str actionSureTitle:@"确定" actionCancelTile:nil actionSureBlock:nil actionCancelBlock:nil style:0];
}
@end
